﻿/*----------------------------------------------------------------
  //Copyright (C) 2012 赵国伟
  //文件功能描述：个人中心Ajax请求服务端处理程序。
  //创建人：赵国伟
  //生成时间：2012/04/21 14:15:00
//----------------------------------------------------------------*/
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

using Shopping.Common;
using Shopping.Model;
using Shopping.BLL;

public partial class Home_AS_AjaxHomeCommon : BaseUserPage
{

    private readonly UsersManage bllUser = new UsersManage();
    private readonly UserAddressManage bllAddress = new UserAddressManage();

    private readonly OrdersManage _ordermanage = new OrdersManage();
    protected List<OrderInfo> _orderList = new List<OrderInfo>();
    protected QueryOrdersAsPage OrderasPage = new QueryOrdersAsPage(true);
    protected string Action = "";
    protected int PageSize = 10;
    protected int CurPage = 0;
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            Action = Request.QueryString["Action"] as String;
            switch (Action)
            {
                case "ShowOrderInfo":
                    ShowOrderInfo();
                    break;
                case "PutInShoppingCart":
                    PutInShoppingCart();
                    break;
                case "CancelFavorite":
                    CancelFavorite();
                    break;
                case "ModifyPwd": //调整登录密码;by 赵国伟 2012-08-18
                    ModifyPwd();
                    break;
                case "GetUserAddress": //查询一条用户地址信息；by 赵国伟 2012-08-18
                    GetUserAddress();
                    break;
                case "DeleteUserAddress": //删除一条指定的收货地址；by 赵国伟 2012-08-18
                    DeleteUserAddress();
                    break;
                case "SaveUserAddress":  //保存收货地址； by 赵国伟 2012-08-18
                    SaveUserAddress();
                    break;
            }
        }
    }

    #region "保存收货地址 by 赵国伟 2012-08-18"
    /// <summary>
    /// 保存收货地址；
    /// </summary>
    private void SaveUserAddress()
    {
        //获取客户端提交上来的数据；结构为json结构；
        string jsonData = Request["jsonData"] + "";  //获取传递过来的Json对象数据；
        UserAddress info = null;
        ReturnValue ret = new ReturnValue();
        try
        {
            ///将提交上来的Json格式的数据装化为相应的实体类；
            info = JSONHelper.JsonStringToObject<UserAddress>(jsonData);
        }
        catch (Exception e)
        {
            ret.Message = "数据提取失败！" + e.Message;
            Response.Write(ret.ToJsonString());
            return;
        }
        info.UserID = this.UserID;
        ret = bllAddress.Save(info);
        Response.Write(ret.ToJsonString());
    }
    #endregion

    #region "删除一条指定的收货地址；by 赵国伟 2012-08-08"
    /// <summary>
    /// 删除一条指定的收货地址
    /// </summary>
    private void DeleteUserAddress()
    {
        int UserAddressID = Utility.G<int>("UserAddressID");
        ReturnValue ret = new ReturnValue(ResultType.Fail, "删除失败(未找到记录)！");
        if (bllAddress.Delete(this.UserID, UserAddressID) > 0)
        {
            ret.Result = ResultType.Success;
            ret.Message = "成功！";
        }
        Response.Write(ret.ToJsonString());
    }
    #endregion

    #region "查询一条用户地址信息 by 赵国伟 2012-08-08"
    /// <summary>
    /// 查询一条用户地址信息
    /// </summary>
    private void GetUserAddress()
    {
        int UserAddressID = Utility.G<int>("UserAddressID");

        UserAddressInfo d = bllAddress.GetAddress(UserAddressID);
        if (d != null)
            Response.Write(JSONHelper.ObjectToJson<UserAddressInfo>(d));
    }
    #endregion

    #region "调整登录密码； by 赵国伟 2012-08-18"
    /// <summary>
    /// 调整登录密码；
    /// </summary>
    private void ModifyPwd()
    {
        string oldPassWord = Utility.P("OldPassWord");
        string newPassWord = Utility.P("NewPassWord"); 

        ReturnValue ret = new ReturnValue(ResultType.Fail);
        if (string.IsNullOrEmpty(oldPassWord))
        {
            ret.Message = "原密码不能为空！";
            Response.Write(ret.ToJsonString());
            Response.End();

        }
        if (string.IsNullOrEmpty(newPassWord))
        {
            ret.Message = "新密码不能为空！";
            Response.Write(ret.ToJsonString());
            Response.End();
        }

        //检查下用户的旧密码是否正确；
        if (!bllUser.IsPassWordCorrect(this.UserID, oldPassWord))
        {
            ret.Message = "输入的原密码错误！";
            Response.Write(ret.ToJsonString());
            Response.End();
        }

        //更新新密码；
        ret = bllUser.UpdatePassWord(this.UserID, newPassWord);
        Response.Write(ret.ToJsonString());
    }
    #endregion



  
    private void PutInShoppingCart()
    {
        var goodid = Utility.G<int>("goodsid");
        ShoppingCartsManage smanager = new ShoppingCartsManage();
        GoodsManage gmanage = new GoodsManage();
        var goodsinfo = gmanage.GetGoods(goodid);
        var shoppingcart = new ShoppingCarts();
        shoppingcart.CookiesID = smanager.GetShoppingCartCookieName(UserID);
        shoppingcart.CreateDate = DateTime.Now;
        shoppingcart.GoodsID = goodid;
        shoppingcart.Price = goodsinfo.BaseInfo.SalePrice;
        shoppingcart.Quantity = 1;
        shoppingcart.Discount = 0;
        var result = smanager.Save(shoppingcart);
        Response.Write(JSONHelper.ObjectToJson(result));
        Response.End();
    }
    private void CancelFavorite()
    {
        var goodid = Utility.G<int>("goodsid");
        UserFavoriteManage userfavoritemanage = new UserFavoriteManage();
        var result = userfavoritemanage.Delete(UserID, goodid);
        Response.Write(JSONHelper.ObjectToJson(result));
        Response.End();
    }

    private void ShowOrderInfo()
    {
        PageSize = Utility.G<int>("pagesize");
        CurPage = Utility.G<int>("page");
        string orderstatus = Utility.G<string>("orderstatus");
        DateTime start = Utility.G<DateTime>("start");
        DateTime end = Utility.G<DateTime>("end");
        string paymode = Utility.G<string>("paymode");
        string sendmode = Utility.G<string>("sendmode");
        string keyword = Utility.G<string>("keyword");
        string dateArea = Utility.G<string>("dataarea");

        if (!string.IsNullOrEmpty(dateArea))
        {
            switch (dateArea)
            {
                case "最近一个月订单":
                    start = DateTime.Now.AddMonths(-1);
                    end = DateTime.Now;
                    break;
                case "一个月以前的订单":
                    end = DateTime.Now.AddMonths(-1);
                    break;
                case "三个月以前的订单":
                    end = DateTime.Now.AddMonths(-3);
                    break;
            }
        }
        OrderasPage.CurPage = CurPage;
        OrderasPage.PageSize = PageSize == 0 ? 10 : PageSize;
        if (start.Year != 1)
            OrderasPage.StartDate = start.ToString("yyyy-MM-dd");
        if (end.Year != 1)
            OrderasPage.EndDate = end.ToString("yyyy-MM-dd");
        OrderasPage.PayMode = paymode;
        OrderasPage.SendMode = sendmode;
        OrderasPage.OrderStatus = orderstatus;
        _orderList = _ordermanage.QueryOrderAsPage(OrderasPage);
    }
}